package com.canaan.business.controller.pc.bill;

import com.canaan.business.domain.bill.RefundProductBill;
import com.canaan.business.domain.vo.RefundProductBillExportVo;
import com.canaan.business.domain.vo.RefundProductBillVo;
import com.canaan.business.service.bill.IRefundProductBillService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 退料单Controller
 *
 * @author ruoyi
 * @date 2023-01-31
 */
@Api(tags = "退料单")
@RestController
@RequestMapping("/bill/refund")
public class RefundProductBillController extends BaseController {
    @Resource
    private IRefundProductBillService refundProductBillService;

    /**
     * 查询退料单列表
     */
    @ApiOperation(value = "查询退料单列表")
    @GetMapping("/list")
    public TableDataInfo list(RefundProductBill refundProductBill) {
        startPage();
        List<RefundProductBill> list = refundProductBillService.selectRefundProductBillList(refundProductBill);
        return getDataTable(list);
    }

    /**
     * 导出退料单列表
     */
    @ApiOperation(value = "导出退料单列表")
    @Log(title = "退料单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RefundProductBill refundProductBill) {
        List<RefundProductBillExportVo> list = refundProductBillService.exportList(refundProductBill);
        ExcelUtil<RefundProductBillExportVo> util = new ExcelUtil<>(RefundProductBillExportVo.class);
        util.exportExcel(response, list, "退料单数据");
    }

    /**
     * 获取退料单详细信息
     */
    @ApiOperation(value = "获取退料单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(refundProductBillService.selectRefundProductBillById(id));
    }

    /**
     * 新增退料单
     */
    @ApiOperation(value = "新增退料单")
    @Log(title = "退料单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RefundProductBill refundProductBill) {
        return toAjax(refundProductBillService.insertRefundProductBill(refundProductBill));
    }

    /**
     * 修改退料单
     */
    @ApiOperation(value = "修改退料单")
    @Log(title = "退料单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RefundProductBill refundProductBill) {
        return toAjax(refundProductBillService.updateRefundProductBill(refundProductBill));
    }

    /**
     * 删除退料单
     */
    @ApiOperation(value = "删除退料单")
    @Log(title = "退料单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(refundProductBillService.deleteRefundProductBillByIds(ids));
    }


    /**
     * 新增退料单
     */
    @ApiOperation(value = "新增退料单")
    @Log(title = "新增退料单", businessType = BusinessType.INSERT)
    @PostMapping("/addWithDetail")
    public AjaxResult addWithDetail(@RequestBody RefundProductBillVo refundProductBillVo) {
        return toAjax(refundProductBillService.addWithDetail(refundProductBillVo));
    }

    /**
     * 退料单导入模板
     */
    @ApiOperation(value = "退料单导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RefundProductBillExportVo> util = new ExcelUtil<>(RefundProductBillExportVo.class);
        util.importTemplateExcel(response, "退料单导入模板");
    }

    /**
     * 订单取消
     */
    @ApiOperation(value = "订单取消")
    @Log(title = "订单取消", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody List<Long> ids) {
        return success(refundProductBillService.cancel(ids));
    }

    /**
     * 订单确认取消
     */
    @ApiOperation(value = "订单确认取消")
    @Log(title = "订单取消", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmCancel")
    public AjaxResult cancel(@RequestBody RefundProductBill refundProductBill) {
        if(refundProductBill == null || refundProductBill.getId() == null){
            throw new ServiceException("缺少必要参数");
        }
        return success(refundProductBillService.confirmCancel(refundProductBill));
    }
}
